package com.cskaoyan.javase.io._2char._2conversion;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 使用字符流循环读取文本文件
 *
 * @since 11:39
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Demo3 {
    @Test
    public void readSingleChar() throws IOException {
        // 1.创建字符输入转换流对象
        InputStreamReader reader = new InputStreamReader(new FileInputStream("a.txt"));
        // 2.循环读,单个字符,单个字符的读
        int readData;
        while ((readData = reader.read()) != -1) {
            System.out.println(((char) readData));
        }
        // 3.关闭流
        reader.close();
    }

    @Test
    public void readMultiChar() throws IOException {
        // 1.创建字符输入转换流对象
        InputStreamReader reader = new InputStreamReader(new FileInputStream("a.txt"));
        // 2.循环读,单个字符,单个字符的读
        char[] buf = new char[1024];
        int readCount;
        while ((readCount = reader.read(buf)) != -1) {
            System.out.println(new String(buf, 0, readCount));
        }
        // 3.关闭流
        reader.close();
    }
}
